import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from properties import Properties
from webstring import Template

#p = Properties()
#p.load(open('skype.properties'))
#p.list()
#print p
#print p.items()

class SkypeSendEmail():	
	def send_email(obj, dialogPartner, messageDate, messageIntro, message, from_username):	 
	 #print messageDate
	 #print messageIntro
	 #print message
	 #print from_username
	 
	 p = Properties()
	 p.load(open('skype.properties'))

	 fromEmail=p['fromemail']
	 toEmail=p['email4sync']
	 
	 # Create message container - the correct MIME type is multipart/alternative.
	 msg = MIMEMultipart('alternative')
	 msg['Subject'] = '['+messageDate+']: '+from_username+': '+messageIntro
	 msg['From'] = '"'+dialogPartner+' "'+'@'+p['smtpserver']
	 msg['To'] = toEmail

	 # Create the body of the message (a plain-text and an HTML version).
	 bodyTemplate = Template(p['xmltemplate'])
	 bodyTemplate.head.text = {'messageDate':messageDate, 'skypeUsername':from_username, 'messageIntro':messageIntro}
	 bodyTemplate.message.text = {'message':message}
	 html = bodyTemplate.render()
	 print html

	 # Record the MIME types of both parts - text/plain and text/html.
	 #text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"

	 # Record the MIME types of both parts - text/plain and text/html.
	 #part1 = MIMEText(text, 'plain')
	 part2 = MIMEText(html, 'html')

	 # Attach parts into message container.
	 # According to RFC 2046, the last part of a multipart message, in this case
	 # the HTML message, is best and preferred.
	 #msg.attach(part1)
	 msg.attach(part2)

	 # Send the message via local SMTP server.
	 s = smtplib.SMTP(p['smtpserver'],p['smtoport'])
	 # sendmail function takes 3 arguments: sender's address, recipient's address
	 # and message to send - here it is sent as one string.
	 s.ehlo()
	 s.starttls()
	 s.ehlo()
	 s.login(p['smtpuser'],p['smtppassword'])
	 
	 print fromEmail
	 print toEmail
	 
	 s.sendmail(fromEmail, toEmail, msg.as_string())
	 s.quit()
